package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zfdang.multiple_images_selector.SelectorSettings;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.LookImgGridAdapter;
import zj.com.adapter.UploadImgGridAdapter;
import zj.com.model.Gjgx;
import zj.com.model.JcxDB;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/9.
 *
 * 关键工序编辑
 */
public class DqrSggxEditActivity extends  BaseActivity implements View.OnClickListener {


    public TextView title_name,right_tv;
    public ImageView left_iv;

    public GridView uploadGridView;
    public List<String> list;
    public LookImgGridAdapter adapter;
    public static final int REQUEST_CODE = 732;

    public TextView gs_tv,xm_tv;
    public EditText question_et;

    public RadioGroup rg;
    public EditText remark_et;

    public EditText ps_et;


    public  static ArrayList<String> mResults = new ArrayList<>();
    public TextView one_tv,two_tv,three_tv,four_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dqr_sggx_edit);
        initView();

    }

    public  Gjgx gjgx;

    public void initView(){
        one_tv = (TextView) findViewById(R.id.one_tv);
        two_tv = (TextView) findViewById(R.id.two_tv);
        three_tv = (TextView) findViewById(R.id.three_tv);
        four_tv = (TextView) findViewById(R.id.four_tv);
        gs_tv = (TextView) findViewById(R.id.gs_tv);
        xm_tv = (TextView) findViewById(R.id.xm_tv);
        question_et = (EditText) findViewById(R.id.question_et);
        remark_et = (EditText) findViewById(R.id.remark_et);
        rg = (RadioGroup) findViewById(R.id.rg);
        ps_et = (EditText) findViewById(R.id.ps_et);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("中交三通");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("确认");
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);


        uploadGridView = (GridView) findViewById(R.id.uploadGridView);

        list = new ArrayList<>();

        adapter = new LookImgGridAdapter(list,this);
        uploadGridView.setAdapter(adapter);

        uploadGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(DqrSggxEditActivity.this,LookImgActivity.class);
                intent.putExtra("imgs",(Serializable)list);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });

        gjgx = (Gjgx) getIntent().getSerializableExtra("gjgx");
        if(gjgx != null){

            xm_tv.setText(gjgx.getProjectName());
            gs_tv.setText(gjgx.getUnitName());
            one_tv.setText("1. "+gjgx.getSpecialProject());
            two_tv.setText("2. "+gjgx.getUnitProject());
            three_tv.setText("3. "+gjgx.getPartProject());
            four_tv.setText("4. "+gjgx.getName());
            title_name.setText(gjgx.getUnitName());
            list  =   gjgx.getAttachUrl();
            remark_et.setText(gjgx.getSPRemark());
            int status =   gjgx.getStatus();
            if(list != null){
                adapter.setDataChange(list);
            }
            if (status == 0){
                rg.check(R.id.btn_0);
            }else if(status == 1){
                rg.check(R.id.btn_1);
            }else if(status == 2){
                rg.check(R.id.btn_2);

            }
        }
    }





    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                doSure();
                break;
        }
    }

    public ProgressDialog dialog;
    public  void doSure(){
        String uid = (String) SharedPreferencesUtil.getData(this, "uid", "");

        dialog = DialogUtil.createProgressDialog(this,"提示","处理中..");
        String request = "";
        JSONObject obj = new JSONObject();
        try
        {
            obj.put("UserID",uid);
            obj.put("ProcessId",gjgx.getID());
            obj.put("Remark",ps_et.toString());
        }catch(Exception e){

        }

        NetManager.getInstance(this).Confirm(obj.toString(), new OnResponseListener<List<JcxDB>>() {
            @Override
            public void onComplete(boolean isOK, List<JcxDB> result, int errorCode, String errorMsg) {

                if(isOK){
                    Toast.makeText(DqrSggxEditActivity.this, "处理成功", Toast.LENGTH_SHORT).show();
                    finish();

                }else{
                    Toast.makeText(DqrSggxEditActivity.this, "处理失败", Toast.LENGTH_SHORT).show();

                }
                dialog.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
                Toast.makeText(DqrSggxEditActivity.this, "处理失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
